package Controller;

import View.MyFrame;
import Model.TrafficLight;
import Model.Car;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author Simon Marshall
 * @author jc209673
 */
public class Simulator {

    private final Timer timer;
    private TrafficLight horizontaltrafficLight;
    private TrafficLight verticaltrafficLight;
    private MyFrame frame;

    /**
     * Creates the Lights, Frame, Timer, lightChange and initializes them within the GUI
     */
    public Simulator() {

        horizontaltrafficLight = new TrafficLight();
        //horizontaltrafficLight.setColor(TrafficLight.GREEN);
        horizontaltrafficLight.setColor(TrafficLight.RED);
        //horizontaltrafficLight.setColor(TrafficLight.YELLOW);

        verticaltrafficLight = new TrafficLight();
        verticaltrafficLight.setColor(TrafficLight.GREEN);
        verticaltrafficLight.setColor(TrafficLight.RED);
        verticaltrafficLight.setColor(TrafficLight.YELLOW);

        frame = new MyFrame();
        frame.setup(horizontaltrafficLight, verticaltrafficLight);
        frame.setVisible(true);

        timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                lightChange();
            }
        }, 3000, 7000);
    }

    private void lightChange() {
        if (horizontaltrafficLight.getColor() == TrafficLight.RED) {
            horizontaltrafficLight.setColor(TrafficLight.GREEN);
        } else if (horizontaltrafficLight.getColor() == TrafficLight.GREEN) {
            horizontaltrafficLight.setColor(TrafficLight.YELLOW);
        } else if (horizontaltrafficLight.getColor() == TrafficLight.YELLOW) {
            horizontaltrafficLight.setColor(TrafficLight.RED);
        }

        if (verticaltrafficLight.getColor() == TrafficLight.RED) {
            verticaltrafficLight.setColor(TrafficLight.GREEN);
        } else if (verticaltrafficLight.getColor() == TrafficLight.GREEN) {
            verticaltrafficLight.setColor(TrafficLight.YELLOW);
        } else if (verticaltrafficLight.getColor() == TrafficLight.YELLOW) {
            verticaltrafficLight.setColor(TrafficLight.RED);
        }
    }

    /**
     *
     * @param args initializes and runs the simulator
     */
    public static void main(String[] args) {
        new Simulator();
    }
}
